using System.Linq.Expressions;
using zhwy.saas.api.Extensions.Log;
using zhwy.saas.api.Repository;

namespace zhwy.saas.api.Services
{
    /// <summary>
    /// 基础服务实现
    /// </summary>
    /// <typeparam name="TEntity">实体类型</typeparam>
    public class BaseService<TEntity> : IBaseService<TEntity> where TEntity : class, new()
    {
        protected readonly IBaseRepository<TEntity> _repository;
        protected readonly ILogExt _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="repository">仓储接口</param>
        /// <param name="logger">日志服务</param>
        public BaseService(IBaseRepository<TEntity> repository, ILogExt logger)
        {
            _repository = repository;
            _logger = logger;
        }

        /// <summary>
        /// 根据ID获取实体
        /// </summary>
        /// <param name="id">实体ID</param>
        /// <returns>实体信息</returns>
        public virtual async Task<TEntity> GetByIdAsync(object id)
        {
            return await _repository.GetByIdAsync(id);
        }

        /// <summary>
        /// 获取所有实体列表
        /// </summary>
        /// <returns>实体列表</returns>
        public virtual async Task<List<TEntity>> GetAllAsync()
        {
            return await _repository.GetAllAsync();
        }

        /// <summary>
        /// 分页获取实体列表
        /// </summary>
        /// <param name="predicate">查询条件</param>
        /// <param name="pageIndex">页码，从1开始</param>
        /// <param name="pageSize">每页记录数</param>
        /// <returns>分页数据</returns>
        public virtual async Task<(List<TEntity> Items, int Total)> GetPagedListAsync(Expression<Func<TEntity, bool>> predicate, int pageIndex, int pageSize)
        {
            return await _repository.GetPagedListAsync(predicate, pageIndex, pageSize);
        }

        /// <summary>
        /// 添加实体
        /// </summary>
        /// <param name="entity">实体信息</param>
        /// <returns>是否添加成功</returns>
        public virtual async Task<bool> AddAsync(TEntity entity)
        {
            return await _repository.AddAsync(entity);
        }

        /// <summary>
        /// 添加实体并返回带有自增ID的实体
        /// </summary>
        /// <param name="entity">实体信息</param>
        /// <returns>添加后的实体（包含自增ID）</returns>
        public virtual async Task<TEntity> AddEntityAsync(TEntity entity)
        {
            return await _repository.AddEntityAsync(entity);
        }

        /// <summary>
        /// 更新实体
        /// </summary>
        /// <param name="entity">实体信息</param>
        /// <returns>是否更新成功</returns>
        public virtual async Task<bool> UpdateAsync(TEntity entity)
        {
            return await _repository.UpdateAsync(entity);
        }

        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="id">实体ID</param>
        /// <returns>是否删除成功</returns>
        public virtual async Task<bool> DeleteAsync(object id)
        {
            return await _repository.DeleteByIdAsync(id);
        }
    }
}